| Conditions | 4 |
| Paths | 3 |
| Total Lines | 56 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 4 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | /* |
||
| 53 | function fsnipDo (cmdOpts, inputText) { |
||
| 54 | // does the processing of the fsnip command |
||
| 55 | // inputText is the text we want to modify |
||
| 56 | if (cmdOpts === null || cmdOpts.length === 0) { return inputText } // no processing required as no options passed in |
||
| 57 | var src = { // a temporary structure containing the text we are working on its type eg. 'json' (which is set later) |
||
| 58 | text: inputText, |
||
| 59 | type: '', |
||
| 60 | outputOptions: {}, |
||
| 61 | error: [], |
||
| 62 | json: null, |
||
| 63 | plain: null |
||
| 64 | } |
||
| 65 | |||
| 66 | parseOptions() |
||
| 67 | postProcess(src) |
||
| 68 | return src.error.length === 0 ? src.text : chalk.redBright(src.error) |
||
| 69 | |||
| 70 | function parseOptions () { |
||
| 71 | // now we are going to parse through the options and arguments to extract individual options together with their arguments |
||
| 72 | var cmdOpt = '' // current option from the cmdOptsString list |
||
| 73 | var cmdArgs = [] // array containing any arguments for the cmdOpt |
||
| 74 | for (var i = 0; i < cmdOpts.length; i++) { |
||
| 75 | if (cmdOpts[i].substr(0, 2) === '--') { // this is a new option eg. --ellipsify |
||
| 76 | processOption() |
||
| 77 | cmdOpt = cmdOpts[i] // store the new option we have found |
||
| 78 | cmdArgs = [] // reset ready for any new arguments |
||
| 79 | } else { |
||
| 80 | // this must be an argument for the current option |
||
| 81 | if (cmdOpt === '') { // error if we don't currently have an option |
||
| 82 | src.error.push("invalid argument '" + cmdOpts[i] + "' passed without valid option to fsnip") |
||
| 83 | } else { |
||
| 84 | cmdArgs.push(cleanCmdOpt(cmdOpts[i])) |
||
| 85 | } |
||
| 86 | } |
||
| 87 | } |
||
| 88 | processOption() |
||
| 89 | |||
| 90 | function processOption () { |
||
| 91 | // process/run any option we've found |
||
| 92 | if (cmdOpt !== '') { |
||
| 93 | runOption(cmdOpt, cmdArgs, src) |
||
| 94 | } |
||
| 95 | } |
||
| 96 | |||
| 97 | function cleanCmdOpt (cmdOpt) { |
||
| 98 | // deals with any special characters dealt with for cross platform purposes |
||
| 99 | // in windows \$ is replaced with $ as $ is a special character which has to be delimited in posix |
||
| 100 | let r = cmdOpt |
||
| 101 | /* istanbul ignore if */ |
||
| 102 | if (os.platform() === 'win32') { |
||
| 103 | r = r.replace(/\\\$/g, '$') |
||
| 104 | } |
||
| 105 | return r |
||
| 106 | } |
||
| 107 | } |
||
| 108 | } |
||
| 109 | |||
| 131 |